package com.eastfair.file.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.eastfair.boot.core.config.BaseConfig;
import com.eastfair.file.props.HuaweiObsProperties;
import com.eastfair.file.props.MinioConfig;
import com.eastfair.file.props.OssProperties;
import com.eastfair.file.service.FileService;
import com.eastfair.file.service.impl.AliYunOssServiceImpl;
import com.eastfair.file.service.impl.HuaweiObsServiceImpl;
import com.eastfair.file.service.impl.MinioFileServiceImpl;
import com.obs.services.ObsClient;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * base配置，里面包含mvc 拦截器,这样包含springUtils实体
 *
 * @author ligang
 * @date 2017 -12-15 14:42
 */
@Configuration
public class FileWebConfiguration extends BaseConfig {

    @Resource
    private MinioConfig minioConfig;

    @Resource
    private HuaweiObsProperties huaweiObsProperties;

    @Resource
    private OssProperties ossProperties;

    /**
     * 构建minio文件服务.
     *
     * @return the file service
     */
    @ConditionalOnProperty(prefix = "minio", name = "enable")
    @Bean
    public FileService buildMinioFileService() {
        MinioFileServiceImpl fileService = new MinioFileServiceImpl();
        fileService.setClient(getMinioClient());
        fileService.setConfig(minioConfig);
        return fileService;
    }

    /**
     * 构建华为obs服务.
     *
     * @return the file service
     */
    @ConditionalOnProperty(prefix = "huawei-obs", name = "enable")
    @Bean
    public FileService buildHuaweiObsService() {
        HuaweiObsServiceImpl fileService = new HuaweiObsServiceImpl();
        fileService.setObsClient(buildObsClient());
        return fileService;
    }

    /**
     * 构建阿里云文件上传服务.
     *
     * @return the file service
     */
    @ConditionalOnProperty(prefix = "oss", name = "enable")
    @Bean
    public FileService buildAliYunOssService() {
        AliYunOssServiceImpl fileService = new AliYunOssServiceImpl();
        fileService.setOssClient(buildAliYunOss());
        fileService.setOssProperties(ossProperties);
        return fileService;
    }

    /**
     * 构建华为Obs客户端.
     *
     * @return the obs client
     */
    @ConditionalOnProperty(prefix = "huawei-obs", name = "enable")
    @Bean
    public ObsClient buildObsClient() {
        return new ObsClient(huaweiObsProperties.getAk(), huaweiObsProperties.getSk(), huaweiObsProperties.getEndPoint());
    }

    /**
     * 构建 Minio 客户端.
     *
     * @return the minio client
     */
    @ConditionalOnProperty(prefix = "minio", name = "enable")
    @Bean
    public MinioClient getMinioClient() {
        return MinioClient.builder().endpoint(minioConfig.getUrl()).credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();
    }

    /**
     * 构建AliYun Oss.
     *
     * @return the oss
     */
    @ConditionalOnProperty(prefix = "oss", name = "enable")
    @Bean
    public OSS buildAliYunOss() {
        return new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey());
    }
}
